﻿//  Key Tracker - An application to keep track of product keys.
//
//  Copyright (c) 2009  Shawn Rakowski
//
//  Permission is hereby granted, free of charge, to any person
//  obtaining a copy of this software and associated documentation
//  files (the "Software"), to deal in the Software without
//  restriction, including without limitation the rights to use,
//  copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following
//  conditions:
//
//  The above copyright notice and this permission notice shall be
//  included in all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//  OTHER DEALINGS IN THE SOFTWARE.
// --------------------------------------------------------------------------//
/*      EditKeysForm.cs 
 *      
 * 
 *      Key Tracker Project   
 *      
 *      CODE FILE DESCRIPTION:
 *      
 *          This code file contains all the classes and methods for the
 *          edit keys form that is run when the edit keys button is pushed
 *          on the main form.
 * 
 *      AUTHOR(S):
 *      
 *          Shawn Rakowski
 * 
 *      CLASSES:
 *      
 *          EditKeysForm : Form
 *      
 *      MODIFICATION HISTORY:
 *      
 *          06-AUG-2008     Shawn Rakowski      New Program.
 *          02-FEB-2009     Shawn Rakowski      added auto-hyphen box
 *          17-MAR-2009     Shawn Rakowski      Changed font, resized a bit, and now only shows
 *                                              the product name, not "Editing for product".
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KeyTracker.Classes;

namespace KeyTracker.Forms
{
    public partial class EditKeysForm : Form
    {
        #region Class Level Variables
        int prodIndex;
        string productName;
        KeyTrackerDatabase keyTrackerDB;
        #endregion

        /// <summary>
        /// Construct an Edit Keys Form.
        /// </summary>
        /// <param name="product"></param>
        /// <param name="productIndex"></param>
        /// <param name="keyTrackerDatabase"></param>
        public EditKeysForm(string product, int productIndex, KeyTrackerDatabase keyTrackerDatabase)
        {
            productName = product;
            prodIndex = productIndex;
            keyTrackerDB = keyTrackerDatabase;
            InitializeComponent();
        }

        private void EditKeysForm_Load(object sender, EventArgs e)
        {
            if (productName.Length > 40)
                productLabel.Text = productName.Substring(0, 40) + "...";
            else
                productLabel.Text = productName;
            autoHyphenCheckBox.Checked = Properties.Settings.Default.AutoHyphenByDefault;
            existingKeysListBox.Items.AddRange(keyTrackerDB.GetKeys(prodIndex));
            ahSectionNumericUpDown.Value = Properties.Settings.Default.AutoHyphenLength;
        }

        private void keyTextBox_TextChanged(object sender, EventArgs e)
        {
            if (keyTextBox.Text != "")
                addKeyButton.Enabled = true;
            else
                addKeyButton.Enabled = false;
        }

        private void addKeyButton_Click(object sender, EventArgs e)
        {
            if (keyTextBox.Text != "")
            {
                if (!existingKeysListBox.Items.Contains(keyTextBox.Text))
                {
                    keyTrackerDB.AddKey(prodIndex, keyTextBox.Text);
                    existingKeysListBox.Items.Add(keyTextBox.Text);
                }
                else
                {
                    string message = "That key already exists for this product!\nKey was not added.";
                    MessageBox.Show(message, "Add Key", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                keyTextBox.Text = "";
            }
        }

        private void finishedButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void existingKeysListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (existingKeysListBox.SelectedIndex != -1)
            {
                deleteKeyButton.Enabled = true;
            }
            else
            {
                deleteKeyButton.Enabled = false;
            }
        }

        private void deleteKeyButton_Click(object sender, EventArgs e)
        {
            if (existingKeysListBox.SelectedIndex != -1)
            {
                string warning = "Are you sure you want to delete this? This will permanently delete this key from the database.";
                if (MessageBox.Show(warning, "Delete Key", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    keyTrackerDB.DeleteKey(prodIndex, existingKeysListBox.SelectedItem.ToString());
                    existingKeysListBox.Items.Clear();
                    existingKeysListBox.Items.AddRange(keyTrackerDB.GetKeys(prodIndex));
                }
            }
        }

        private void AddHyphens()
        {
            keyTextBox.Text = keyTextBox.Text.Replace("-", "");
            int length = keyTextBox.Text.Length;
            int len = 0;
            int modifier = 0;
            int addedHyphens = 0;
            while (len < length - ahSectionNumericUpDown.Value)
            {
                len += (int)ahSectionNumericUpDown.Value;
                keyTextBox.Text = keyTextBox.Text.Insert(len + modifier, "-");
                modifier++;
                addedHyphens++;
            }
            keyTextBox.SelectionStart = length + addedHyphens + 1;
        }

        private void autoHyphenCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (autoHyphenCheckBox.Checked)
                AddHyphens();
            else
                if (Properties.Settings.Default.DeleteHyphenWhenAutoHyphenUnchecked)
                    keyTextBox.Text = keyTextBox.Text.Replace("-", "");
            keyTextBox.Focus();
        }

        private void keyTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
            {
                if (autoHyphenCheckBox.Checked)
                    AddHyphens();
            }
            else
            {
                addKeyButton_Click(sender, e);
            }
        }
    }
}
